const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        have_apply: null,
        show_lock: false,
        realname: '',
        phone: '',
        supervise_region_1: null,
        supervise_region_2: null,
        supervise_region_3: null,
        idcard_frontend: '',
        idcard_backend: '',
        idcard_hold: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        // 验证登录
        app.verifyLogin();

    },

    /**
     * 生命周期回调—-监听页面显示
     */
    onShow: function() {

        if(true === this.data.show_lock){
            return;
        };

        app.execute(function() {
            wx.showLoading({
                title: '获取申请...',
            });
            wx.request({
                url: app.data.service_url + 'staff_index/get_apply_status',
                header: {
                    'Client-Token': app.data.client_token
                },
                method: 'GET',
                success: function(response) {
                    wx.hideLoading();
                    if (200 === response.statusCode) {
                        if(0 < response.data.message.length){
                            wx.showToast({
                                title: response.data.message,
                                icon: 'none'
                            });
                        };
                        if (true === response.data.data.is_success) {
                            setTimeout(function(){
                                wx.reLaunch({
                                    url: '/pages/staff_home/index/index',
                                });
                            }, 1500);
                        };
                        let page = app.getPage();
                        page.setData({
                            have_apply: response.data.data.have_apply
                        });
                    } else {
                        wx.showToast({
                            title: '远程服务返回错误',
                            icon: 'none'
                        });
                    };
                },
                fail: function() {
                    wx.hideLoading();
                    wx.showToast({
                        title: '连接远程服务失败',
                        icon: 'none'
                    });
                }
            })
        });

    },

    /**
     * 改变输入框
     */
    changeInput: function(e) {
        let change = new Object();
        change[e.target.dataset.name] = e.detail.value;
        this.setData(change);
    },

    /**
     * 选择地区
     */
    selectRegion: function(e) {
        let change = new Object();
        change[e.currentTarget.dataset.name] = {
            province: e.detail.value[0],
            city: e.detail.value[1],
            district: e.detail.value[2]
        };
        this.setData(change);
    },

    /**
     * 自定义函数--上传图片
     */
    uploadImage: function(e) {

        let data_name = e.currentTarget.dataset.name;
        this.data.show_lock = true;

        wx.showLoading({
            title: '请等待...'
        });
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            success: function(e) {
                if ('chooseImage:ok' === e.errMsg) {
                    let FileSystemManager = wx.getFileSystemManager();
                    if (undefined === FileSystemManager) {
                        wx.hideLoading();
                        wx.showToast({
                            title: '微信版本过低，请升级微信',
                            icon: 'none'
                        });
                        let page = app.getPage();
                        page.data.show_lock = false;
                    } else {
                        // 将文件转为Base64编码
                        wx.getFileSystemManager().readFile({
                            filePath: e.tempFilePaths[0],
                            encoding: 'base64',
                            success: function(e) {
                                wx.hideLoading();
                                let change = new Object();
                                change[data_name] = e.data;
                                let page = app.getPage();
                                page.setData(change);
                                page.data.show_lock = false;
                            },
                            fail: function() {
                                wx.hideLoading();
                                wx.showToast({
                                    title: '图片转码失败，请升级微信至最新版本',
                                    icon: 'none'
                                });
                                let page = app.getPage();
                                page.data.show_lock = false;
                            }
                        });
                    };
                } else {
                    wx.showToast({
                        title: '获取图片失败',
                        icon: 'none'
                    });
                    let page = app.getPage();
                    page.data.show_lock = false;
                };
            },
            fail: function(e) {
                wx.hideLoading();
                wx.showToast({
                    title: '取消上传',
                    icon: 'none'
                });
                let page = app.getPage();
                page.data.show_lock = false;
            }
        });
    },

    submitForm: function() {

        let data = {
            realname: this.data.realname,
            phone: this.data.phone,
            supervise_region_1: this.data.supervise_region_1,
            supervise_region_2: this.data.supervise_region_2,
            supervise_region_3: this.data.supervise_region_3,
            idcard_frontend: this.data.idcard_frontend,
            idcard_backend: this.data.idcard_backend,
            idcard_hold: this.data.idcard_hold
        };

        if (0 === data.realname.length) {
            wx.showToast({
                title: '姓名不能为空',
                icon: 'none'
            });
            return;
        };
        if (true !== /^[\u4e00-\u9fa5]*$/.test(data.realname)) {
            wx.showToast({
                title: '姓名只能为汉字',
                icon: 'none'
            });
            return;
        };

        if (0 === data.phone.length) {
            wx.showToast({
                title: '联系电话不能为空',
                icon: 'none'
            });
            return;
        };
        if (true !== /^[0-9]{11}$/.test(data.phone)) {
            wx.showToast({
                title: '联系电话只能为11位数字',
                icon: 'none'
            });
            return;
        };

        if (null === data.supervise_region_1) {
            wx.showToast({
                title: '必须设置第一个负责地区',
                icon: 'none'
            });
            return;
        };

        if (0 === data.idcard_frontend.length) {
            wx.showToast({
                title: '必须上传身份证正面照',
                icon: 'none'
            });
            return;
        };
        if (0 === data.idcard_backend.length) {
            wx.showToast({
                title: '必须上传身份证背面照',
                icon: 'none'
            });
            return;
        };
        if (0 === data.idcard_hold.length) {
            wx.showToast({
                title: '必须上传身份证持证照',
                icon: 'none'
            });
            return;
        };

        wx.showLoading({
            title: '处理中...',
        });
        wx.request({
            url: app.data.service_url + 'staff_index/make_apply',
            header: {
                'Client-Token': app.data.client_token
            },
            data: data,
            method: 'POST',
            success: function(response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (0 === response.data.errno) {
                        wx.showToast({
                            title: response.data.message,
                        });
                        let page = app.getPage();
                        page.setData({
                            have_apply: true,
                        });
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    }

});